import javax.jms.*;
import java.util.ArrayList;
import java.util.Random;
import org.apache.activemq.ActiveMQConnectionFactory;
public class signalPublish {
    private static String brokerURL = "tcp://localhost:61616";
    private static ConnectionFactory factory;
    private Connection connection;
    private Session session;
    private MessageProducer producer;
    private Topic topic;
    private int epoch=1000;

    public signalPublish(String topicName) throws JMSException {
        factory = new ActiveMQConnectionFactory(brokerURL);
        connection = factory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        topic = session.createTopic(topicName);
        producer = session.createProducer(topic);
        connection.start();
    }

    public void sendMessage() throws JMSException {
        for(int i=0;i<epoch;i++){
            Random random=new Random();
            double num=random.nextGaussian();
            StreamMessage message = session.createStreamMessage();
            message.writeDouble(num);
            producer.send(message);
            System.out.println(num);
            try{
                Thread.sleep(100);
            }catch (Exception e){
                System.out.println("error"+e);
            }
        }
    }

    public void close() throws JMSException {
        if (connection != null) {
            connection.close();
        }
        if (session!=null){
            session.close();
        }
    }

    public static void main(String[] args) throws JMSException {
        signalPublish publisher = new signalPublish("randomSignal");
        publisher.sendMessage();
        publisher.close();
    }
}
